package com.example.wxchatdemo;


import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private EditText account,password;
    private Button login,register;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        account = findViewById(R.id.account);
        password = findViewById(R.id.password);
        login = findViewById(R.id.login);
        register = findViewById(R.id.register);
        /***
         * 登录按钮点击事件
         */
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 开一个线程完成网络请求操作
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String result = null;
                        OkHttpClient client = new OkHttpClient();
                        Request request = new Request.Builder()
                                .url("http://100.2.157.154:8099/user/login?phone="+account.getText()+"&password="+password.getText())
                                .build();

                        try (Response response = client.newCall(request).execute()) {
                            result = response.body().string();
                            String finalResult = result;
                            JSONObject obj = JSON.parseObject(finalResult);

                            //SharedPreferences使用
                            SharedPreferences sp = getSharedPreferences("config",0);
                            SharedPreferences.Editor editor=sp.edit();
                            //把数据进行保存
                            editor.putString("uphone",obj.getString("phone"));
                            editor.commit();


                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
//                                    if (finalResult.equals("2")){
//                                        Toast.makeText(getApplicationContext(), "登录成功",
//                                                Toast.LENGTH_SHORT).show();
//                                        Intent intent1 = new Intent(LoginActivity.this,NavigationActivity.class);
//                                        startActivity(intent1);
//                                    }
                                    if(finalResult.equals("登录失败，该号码未注册")){
                                        Toast.makeText(getApplicationContext(), "登录失败,该号码未注册",
                                                Toast.LENGTH_SHORT).show();

                                    }else if(finalResult.equals("登录失败，密码错误")){
                                        Toast.makeText(getApplicationContext(), "登录失败,密码错误",
                                                Toast.LENGTH_SHORT).show();
                                    }else {

                                        Toast.makeText(getApplicationContext(), "登录成功",
                                                Toast.LENGTH_SHORT).show();
                                        Intent intent1 = new Intent(MainActivity.this,Main_one.class);
                                        startActivity(intent1);


                                    }
                                }
                            });



                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
        /***
         * 注册按钮点击事件
         */
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

    }




}